package com.chaopei.day03;

/**
 * @Author Corday 天涯
 * @Date 2022/3/7 9:44
 */
public class Mymain {
    public static void main(String[] args) {
        MyAbstract my1 = new Person(1001,"汤姆","男",8900);
        MyAbstract my2 = new Person(1002,"小汤","男",8300);
        MyAbstract my3 = new Person(1003,"tom","男",7800);
        MyAbstract my4 = new Person(1004,"bean","男",8100);
        MyAbstract[] mys ={my1,my2,my3,my4};
        mySort(mys);
        dispArr(mys);
        Pet pet = new Pet(101,"冰墩墩");
        Toy toy = new Toy(105,"玩具兵",150.0);
        Pet toy1 = new Toy(102,"风筝",20d);
        TomCat tomCat = new TomCat(101,"汤姆","可爱");
        Pet[] pets ={pet,toy,toy1,tomCat};
//        多态数组输出
        System.out.println("------------------");
        travelArr(pets);

    }
//    实现数组中对象的排序
    public  static  void mySort(MyAbstract[] arr){
        for(int i=0;i<arr.length;i++){
            for(int j=i;j<arr.length;j++){
               MyAbstract temp = arr[i];
               if(((Person)arr[i]).getMoney()<((Person)arr[j]).getMoney()){
                    arr[i]=arr[j];
                    arr[j]=temp;
               }
            }
        }
    }
//    遍历数组
    public  static  void dispArr(MyAbstract[] arr){
        for(MyAbstract temp:arr){
            System.out.println(((Person)temp).getMoney());
        }
    }
//    排序后遍历数组
    public  static  void travelArr(Pet[] pets){
        for(int i=0;i<pets.length;i++){
            for(int j=0;j<pets.length-i-1;j++){
                Pet temp = pets[j];
                if(pets[j].getPid()>pets[j+1].getPid()){
                    pets[j]=pets[j+1];
                    pets[j+1]=temp;
                }
            }//for1
        }//for2
        for(Pet pet:pets){
            System.out.println("编号:"+pet.getPid()+"名称:"+pet.getName());
        }
    }
//    创建一个新的对象,并添加到有序的pets数组中,让此数组中的对象中的id号有序
}
